package com.fagejiang.nebula.client;

import com.fagejiang.nebula.DbUtil;
import com.fagejiang.nebula.entity.Alipay;
import com.fagejiang.nebula.entity.AlipayDto;
import com.fagejiang.nebula.entity.TransferOfAlipay;
import com.vesoft.nebula.client.graph.NebulaPoolConfig;
import com.vesoft.nebula.client.graph.data.HostAddress;
import com.vesoft.nebula.client.graph.data.ResultSet;
import com.vesoft.nebula.client.graph.exception.AuthFailedException;
import com.vesoft.nebula.client.graph.exception.IOErrorException;
import com.vesoft.nebula.client.graph.exception.NotValidConnectionException;
import com.vesoft.nebula.client.graph.net.NebulaPool;
import com.vesoft.nebula.client.graph.net.Session;
import org.junit.Before;
import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

/**
 * TODO
 *
 * @author 发哥讲Java
 * @version 1.0
 * @date 2021-04-22 18:14
 */
public class TestInsertMain {
    NebulaPool pool;
    Session session;

    @Before
    public void before() throws UnknownHostException, NotValidConnectionException, IOErrorException, AuthFailedException {
        NebulaPoolConfig nebulaPoolConfig = new NebulaPoolConfig();
        nebulaPoolConfig.setMaxConnSize(10);
        List<HostAddress> addresses = Collections.singletonList(new HostAddress("192.168.0.102", 9669));
        pool = new NebulaPool();
        pool.init(addresses, nebulaPoolConfig);
        session = pool.getSession("root", "nebula", false);
    }

    @Test
    public void mainTask() throws IOErrorException, UnsupportedEncodingException, SQLException {
        String useSpace = " use yeyingtest; ";
        StringBuilder insertSql = new StringBuilder(useSpace);
        final AlipayDto transfer = getTransfer();
        final List<Alipay> alipays = transfer.getAlipays();
        ResultSet execute;
        final long start = System.currentTimeMillis();
//        for (int i = 0; i < alipays.size(); i++) {
//            final Alipay alipay = alipays.get(i);
//            insertSql.append(" UPSERT VERTEX '" + alipay.getVid() + "' set Alipay.name ='" + alipay.getName() + "' ," +
//                    " Alipay.nickName='" + alipay.getNickName() + "',  Alipay.account='" + alipay.getAccount() + "' ; ");
//            if (i % 500 == 0) {
//                execute = session.execute(insertSql.toString());
//                System.out.println("execute = " + execute.isSucceeded());
//                System.out.println(execute.getErrorMessage());
//                insertSql = new StringBuilder(useSpace);
//            }
//        }
        insertSql = new StringBuilder(useSpace);
        final List<TransferOfAlipay> ofAlipays = transfer.getTransferOfAlipays();
        for (int i = 0; i < ofAlipays.size(); i++) {
            final TransferOfAlipay ofAlipay = ofAlipays.get(i);
//            insertSql.append(" UPSERT EDGE  '" + ofAlipay.getAId() + "' -> '" + ofAlipay.getBId() + "' of TransferOfAlipay " +
//                    " set totalTransfer =TransferOfAlipay.totalTransfer+" + ofAlipay.getTotalTransfer() + " , " +
//                    " totalAmount=totalTransfer.totalAmount+" + ofAlipay.getTotalAmount() + " ; ");
            insertSql.append(" insert edge TransferOfAlipay(totalTransfer,totalAmount) values  '" + ofAlipay.getAId() + "' -> '" + ofAlipay.getBId() + "'  " +
                    " :( " + ofAlipay.getTotalTransfer() + " , " +
                    "" + ofAlipay.getTotalAmount() + " ); ");
            if (i % 500 == 0) {
                execute = session.execute(insertSql.toString());
                System.out.println("execute = " + execute.isSucceeded());
                System.out.println(execute.getErrorMessage());
                System.out.println("insertSql = " + insertSql);
                insertSql = new StringBuilder(useSpace);
            }
        }

        System.out.println("time:" + (System.currentTimeMillis() - start) / 1000);
    }

    public AlipayDto getTransfer() throws SQLException {
        AlipayDto alipayDto = new AlipayDto();
        final Connection connection = DbUtil.getConnection();
        System.out.println("connection = " + connection);
        String sql = "select transfer_pay_account,transfer_pay_account_id,transfer_collection_account,transfer_collection_account_id," +
                " transfer_collection_account_name,transfer_pay_account_name,transfer_pay_name,transfer_collection_name,transfer_pay_fee from anti_fraud_transfer_all limit 10000 ;";
        final PreparedStatement statement = connection.prepareStatement(sql);
        final java.sql.ResultSet rs = statement.executeQuery();
        while (rs.next()) {
            final Alipay a = new Alipay();
            a.setVid("zfb" + rs.getString("transfer_pay_account"));
            a.setAccount(rs.getString("transfer_pay_account_id"));
            a.setName(rs.getString("transfer_pay_name"));
            a.setNickName(rs.getString("transfer_pay_account_name"));
            alipayDto.getAlipays().add(a);
            final Alipay b = new Alipay();
            b.setVid("zfb" + rs.getString("transfer_collection_account"));
            b.setAccount(rs.getString("transfer_collection_account_id"));
            b.setName(rs.getString("transfer_collection_name"));
            b.setNickName(rs.getString("transfer_collection_account_name"));
            alipayDto.getAlipays().add(b);
            final TransferOfAlipay ofAlipay = new TransferOfAlipay();
            ofAlipay.setTotalAmount(rs.getDouble("transfer_pay_fee"));
            ofAlipay.setTotalTransfer(1);
            ofAlipay.setAId("zfb" + rs.getString("transfer_pay_account"));
            ofAlipay.setBId("zfb" + rs.getString("transfer_collection_account"));
            alipayDto.getTransferOfAlipays().add(ofAlipay);
        }
        rs.close();
        statement.close();
        connection.close();
        return alipayDto;
    }

    @Test
    public void after() {
        session.release();
        pool.close();
    }

}
